package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TArticle;
import com.cookor.poi.modules.phy.service.TArticleService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tArticle"})
public class TArticleController extends BaseController {
  @Autowired
  private TArticleService tArticleService;
  
  @ModelAttribute
  public TArticle get(@RequestParam(required = false) String id) {
    TArticle entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tArticleService.get(id); 
    if (entity == null)
      entity = new TArticle(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tArticle:view"})
  @RequestMapping({"list", ""})
  public String list(TArticle tArticle, HttpServletRequest request, HttpServletResponse response, Model model) {
    tArticle.setTenantId(UserUtils.getTenantId());
    Page<TArticle> page = this.tArticleService.findPage(new Page(request, response), tArticle);
    model.addAttribute("tArticle", tArticle);
    model.addAttribute("page", page);
    return "modules/phy/tArticleList";
  }
  
  @RequiresPermissions({"phy:tArticle:view"})
  @RequestMapping({"form"})
  public String form(TArticle tArticle, Model model) {
    model.addAttribute("tArticle", tArticle);
    model.addAttribute("admin", UserUtils.getUser());
    return "modules/phy/tArticleForm";
  }
  
  @RequiresPermissions({"phy:tArticle:edit"})
  @RequestMapping({"save"})
  public String save(TArticle tArticle, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tArticle, new Class[0]))
      return form(tArticle, model); 
    this.tArticleService.save(tArticle);
    addMessage(redirectAttributes, new String[] { "保存图文成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tArticle/?repage";
  }
  
  @RequiresPermissions({"phy:tArticle:edit"})
  @RequestMapping({"delete"})
  public String delete(TArticle tArticle, RedirectAttributes redirectAttributes) {
    this.tArticleService.delete(tArticle);
    addMessage(redirectAttributes, new String[] { "删除图文成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tArticle/?repage";
  }
}
